'''
Describe data set with form
Age,Sex,BMI,Weight,Years Smoking

Give mean, median, var, range, iqr
'''


import sys
if len(sys.argv) < 2:
    print "Usage: python describe_data.py <file>" 
    sys.exit(1)


fileName = sys.argv[1]
fileHandle = open(fileName,'r')

header = fileHandle.readline()

age = []
gender = []
bmi = []
weight = []
yrsSmoking = []

ageM = []
genderM = []
bmiM = []
weightM = []
yrsSmokingM = []

ageF = []
genderF = []
bmiF = []
weightF = []
yrsSmokingF = []

lineNum = 1
for line in fileHandle:
    # tokenize
    tokens = line.rstrip().split(',')
    age.append(float(tokens[0]))
    gender.append(tokens[1])
    bmi.append(float(tokens[2]))
    weight.append(float(tokens[3]))
    yrsSmoking.append(float(tokens[4]))

    if tokens[1] == 'M':

        ageM.append(float(tokens[0]))
        genderM.append(tokens[1])
        bmiM.append(float(tokens[2]))
        weightM.append(float(tokens[3]))
        yrsSmokingM.append(float(tokens[4]))

    elif tokens[1] == 'F':

        ageF.append(float(tokens[0]))
        genderF.append(tokens[1])
        bmiF.append(float(tokens[2]))
        weightF.append(float(tokens[3]))
        yrsSmokingF.append(float(tokens[4]))

    else:
        print "Error in data, line ", lineNum
        sys.exit(1)

    lineNum+=1

print "N: %d %d %d" %(len(age), len(ageM), len(ageF))

print "mean: %f %f %f" %(sum(age) / len(age), sum(ageM) / len(ageM), sum(ageF) / len(ageF))

# Calculate median
age.sort()
ageM.sort()
ageF.sort()

medianAge = 0
medianAgeM = 0
medianAgeF = 0

if len(age) % 2 == 0:
    medianAge = (age[len(age) / 2] + age[len(age)/2+1]) / 2
else:
    medianAge = age[len(age) / 2]

print "median: %f %f %f" % ( medianAge, medianAgeM, medianAgeF )

